import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)
export const createStore = () => {
    return new Vuex.Store({
        state: () => ({
            posts: []
        }),

        mutations:{
            setPosts(state, data){
                state.posts = data
            }
        },

        //commit是context对象数据
        actions:{
            async getPosts({commit}){
                console.log('test1')

                const {data} = await axios.get('https://cnodejs.org/api/v1/topics')
                console.log('test2')
                commit('setPosts',data.data)
            }
        }
    })
}